// components/my-tradeIndexGoods/my-tradeIndexGoods.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    childValue:'输入关键词，搜索想要的商品',
    inputValue:'',
    goodsLis:[
      {id:0,src:'/images/cat.jpg',transactionMethod:'自提',goodsType:'二手书',goodsName:"你是？",price:50,fromAvatar:"/images/avatar.png",fromId:'いち'},
      {id:1,src:"/images/cat.jpg",transactionMethod:"邮递",goodsType:'新书',goodsName:"我是？",price:50,fromAvatar:"/images/avatar.png",fromId:'に'},
      {id:2,src:"/images/cat.jpg",transactionMethod:"邮递",goodsType:'新书',goodsName:"耳机",price:50,fromAvatar:"/images/avatar.png",fromId:'さん'},
      {id:3,src:"/images/cat.jpg",transactionMethod:"自提",goodsType:'新书',goodsName:"饮料",price:50,fromAvatar:"/images/avatar.png",fromId:'よん'}
    ],
    searchLis:[],
    searchKeyword:'',
    good:[]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onInput(e){
      const inputValue=e.detail.value
      this.setData({
        searchKeyword: inputValue,
      });
      if(inputValue!=''){
        const matchedResults = this.searchMatchingResults(inputValue, this.data.goodsLis);
        this.setData({
          searchLis: matchedResults
        });
      }else{
        this.setData({
          searchLis:this.data.goodsLis
        })
      }
    },
    searchMatchingResults: function(keyword, results) {
      // 根据搜索关键词进行匹配判断
      return results.filter(item => {
        const itemName = String(item.goodsName).toLowerCase();
        return itemName.includes(keyword.toLowerCase());
      });
    },
    //传参
    navToCommodity(e){
      console.log(e.target.dataset.id);
      this.setData({
        good:{
          fromAvatar:this.data.goodsLis[e.target.dataset.id].fromAvatar,
          goodDetailPrice:this.data.goodsLis[e.target.dataset.id].price,
          goodDetailTransactionMethod:this.data.goodsLis[e.target.dataset.id].transactionMethod,
          goodDetailName:this.data.goodsLis[e.target.dataset.id].goodsName,
          goodDetailType:this.data.goodsLis[e.target.dataset.id].goodsType,
          goodgoodDetailId:this.data.goodsLis[e.target.dataset.id].id
        }
      })
      // var navUrl='/pages/commodityPage/commodityPage?data='+JSON.stringify(this.data.good)
      var navUrl='/pkgInnerCommodity/pages/commodityPage/commodityPage?data='+JSON.stringify(this.data.good)
      wx.navigateTo({
        url: navUrl,
      })
    }
  },
  lifetimes:{
    ready(){
      this.setData({
        searchLis:this.data.goodsLis
      })
    }
  }
})
